import Mock from 'mockjs'
import store from '@/store'

const tokens = {
  admin: {
    token: 'admin-token'
  },
  editor: {
    token: 'editor-token'
  }
}

const users = {
  'admin-token': {
    age: 17,
    name: 'admin',
    introduction: 'I am a super administrator',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    token: 'SuperAdmin'
  },
  'editor-token': {
    age: 18,
    name: 'editor',
    introduction: '',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    token: 'NormalEditor'
  }
}

export default [
  // user login
  {
    url: '/api/user/login',
    type: 'post',
    response: config => {
      const { userCode } = config.body
      const token = tokens[userCode]?.token
      if (!token) {
        return {
          code: 60204,
          message: '账号或密码错误'
        }
      }
      return {
        code: 200,
        data: users[token]
      }
    }
  },

  // 获取验证码
  {
    url: '/api/user/getcode',
    type: 'post',
    response: config => {
      const { mobile } = config.body
      if (!mobile) {
          return {
              code: 60021,
              message: '缺少参数'
          }
      }
      return {
        code: 200,
        data: Mock.mock(Mock.Random.zip())
      }
    }
  },

   // 验证码登录
  {
    url: '/api/user/LoginBycode',
    type: 'post',
    response: config => {
      const { vcode } = config.body
      const token = tokens['admin']?.token
      if (vcode !== store.getters.vcode) {
        return {
          code: 62201,
          message: '验证码错误'
        }
      }
      return {
        code: 200,
        data: users[token]
      }
    }
  }, 
  // user logout
  {
    url: '/api/user/logout',
    type: 'post',
    response: _ => {
      return {
        code: 200,
        data: 'success'
      }
    }
  }
]